********************************************************************************
*PROJECT: Social interactions of lawmakers and the partisan divide								   
*PURPOSE: Prep MP-session level simulated voting data
********************************************************************************

clear all
pause on
local votingsimfolder = "$path_pch/Data/intermediate/Simulated"
local votinganalyfolder = "$path_pch\Data\analysis\Voting"
timer clear
timer on 1


** Auto-fill 
local pbig = $pcue*100
local pcuelab "`pbig'perc"
local drawstart = $drawstart
local drawend = $drawend


*********************************************************************
// LHS: Vote
*********************************************************************

// Bring voting data created from Saia_prep
use "`votinganalyfolder'/voting_analysis", clear

// Calculate modal vote
keep vote_id vote
bys vote_id vote: gen NumVote = _N
bys vote_id: egen ModalVoteCount = max(NumVote)
bys vote_id: gen totalvote = _N

// collapse, draw histogram
keep vote_id ModalVoteCount totalvote
duplicates drop
gen ModalVoteShare = ModalVoteCount / totalvote

foreach p in p50 p25 {
	preserve
		sum ModalVoteShare, d
		keep if ModalVoteShare < r(`p')
		keep vote_id
		dis _N
		tempfile VS_Vote_`p'
		save `VS_Vote_`p''
	restore
}


// Bring simulated voting data
use "$path_pch/Data/intermediate/Simulated/votes_simulated_RI${drawstart}to${drawend}_`pcuelab'", clear
keep MP_id vote_id vote_inf* vote_natural
tempfile newvote
save `newvote'

// Bring voting data created from Saia_prep
use "`votinganalyfolder'/voting_analysis", clear
keep vote_id session_id bill_id MP_id chairman_vote ///
	leader_vote party_leader_id
merge 1:1 vote_id MP_id using `newvote', assert(3) nogen


// create vote similarity measure
forv i=$drawstart/$drawend {
	gen voteSim4c`i' = 3-abs(3*(vote_inf`i'=="yes")+2*(vote_inf`i'=="absent")+1*(vote_inf`i'=="abstain")-3*(leader_vote=="yes")-2*(leader_vote=="absent")-1*(leader_vote=="abstain")) if !mi(vote_inf`i') & !mi(leader_vote) & MP_id!=party_leader_id
	gen voteSim3c`i' = 2-abs((vote_inf`i'=="yes")-(vote_inf`i'=="no")-(leader_vote=="yes")+(leader_vote=="no")) if !mi(vote_inf`i') & !mi(leader_vote) & MP_id!=party_leader_id

	// old measure using chair of parliamentary group's vote
	gen voteSim4c_saia`i' = 3-abs(3*(vote_inf`i'=="yes")+2*(vote_inf`i'=="absent")+1*(vote_inf`i'=="abstain")-3*(chairman_vote=="yes")-2*(chairman_vote=="absent")-1*(chairman_vote=="abstain")) if !mi(vote_inf`i') & !inlist(chairman_vote,"","unknown")
	gen voteSim3c_saia`i' = 2-abs((vote_inf`i'=="yes")-(vote_inf`i'=="no")-(chairman_vote=="yes")+(chairman_vote=="no")) if !mi(vote_inf`i') & !inlist(chairman_vote,"","unknown")

	// create rebel measure
	g rebelStrong`i' = (vote_inf`i'=="yes" & leader_vote=="no") | (vote_inf`i'=="no" & leader_vote=="yes") if !mi(vote_inf`i') & !mi(leader_vote) & MP_id!=party_leader_id
	g rebelWeak`i' = (vote_inf`i'=="yes" & leader_vote=="no") | (vote_inf`i'=="no" & leader_vote=="yes") | ///
			(vote_inf`i'=="abstain" & leader_vote=="yes") | (vote_inf`i'=="abstain" & leader_vote=="no") if !mi(vote_inf`i') & !mi(leader_vote) & MP_id!=party_leader_id

	// absence
	g absent`i' = vote_inf`i'=="absent" if !mi(vote_inf`i')
	g abstain`i' = vote_inf`i'=="abstain" if !mi(vote_inf`i')
	g absentORabstain`i' = inlist(vote_inf`i',"absent","abstain") if !mi(vote_inf`i')
}

drop vote_inf* chairman_vote leader_vote

// Collapse to get measures: contentious votes (VS, using p50 ~= .66), very contentious votes (VVS, using p25 ~= .52)
forv k = 1/2 {
	local p: word `k' of "p25" "p50"
	local disc: word `k' of "VVS" "VS"
	preserve
		merge n:1 vote_id using `VS_Vote_`p'', nogen keep(3) // keep only contentious votes
		drop vote_id // Drop unnecessary variables
		collapse (mean) voteSim?c* rebelStrong* rebelWeak* absent* abstain*, by(session_id bill_id MP_id) // each bill gets the same weight, so collapse by bill first.
		drop bill_id
		collapse (mean) voteSim?c* rebelStrong* rebelWeak* absent* abstain*, by(session_id MP_id) // now collapse to session-MP-level
		foreach var of varlist voteSim?c* rebelStrong* rebelWeak* absent* abstain* {
			ren `var' `var'_`disc'
		}
		tempfile voting_`disc'
		save `voting_`disc''
	restore 
}

// First, collapse for equal weight for each bill
preserve
	drop vote_id
	collapse (mean) voteSim?c* rebelStrong* rebelWeak* absent* abstain*, by(session_id bill_id MP_id)
	drop bill_id
	collapse (mean) voteSim?c* rebelStrong* rebelWeak* absent* abstain*, by(session_id MP_id)
	tempfile vote_billWeight
	save `vote_billWeight'
restore

// Second, collapse for equal weight for each vote
foreach v of varlist rebelStrong* rebelWeak* voteSim?c* absent* abstain* {
	ren `v' `v'_Vwgt
}

drop bill_id
collapse (mean) *_Vwgt, by(session_id MP_id)

// merge preferred weight variables
merge 1:1 session_id MP_id using `vote_billWeight', nogen
merge 1:1 session_id MP_id using `voting_VVS', nogen
merge 1:1 session_id MP_id using `voting_VS', nogen

*la var non_compliance "frac. voted differently to party leader, weight by bill"
*la var non_compliance_saia "frac. voted differently to chair of parliamentary group (as in Saia), weight by bill"
*la var non_compliance_alt "frac. voted differently to modal party vote, weight by bill"
*la var non_compliance_Vwgt "frac. voted differently to party leader, weight by vote"
*la var non_compliance_saia_Vwgt "frac. voted differently to chair of parliamentary group (as in Saia), weight by vote"
*la var non_compliance_alt_Vwgt "frac. voted differently to modal party vote, weight by vote"
/*
la var voteSim4c "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no), weight by bill"
la var voteSim3c "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no), weight by bill"
la var voteSim4c_saia "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no) (Saia chair), weight by bill"
la var voteSim3c_saia "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no) (Saia chair), weight by bill"
la var voteSim4c_Vwgt "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no), weight by vote"
la var voteSim3c_Vwgt "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no), weight by vote"
la var voteSim4c_saia_Vwgt "ave. 4 Cat vote similarity (3 = same vote, 0 = yes-no) (Saia chair), weight by vote"
la var voteSim3c_saia_Vwgt "ave. 3 Cat vote similarity (1 = same vote, -1 = yes-no) (Saia chair), weight by vote"
la var rebelStrong "frac. voted no (yes) when party leader voted yes (no), weight by bill"
la var rebelStrong_Vwgt "frac. voted no (yes) when party leader voted yes (no), weight by vote"
la var rebelWeak "frac. voted no/abstain (yes/abstain) when party leader voted yes (no), weight by bill"
la var rebelWeak_Vwgt "frac. voted no/abstain (yes/abstain) when party leader voted yes (no), weight by vote"
la var absent "frac. absent from voting, weight by bill"
la var absent_Vwgt "frac. absent from voting, weight by vote"
la var abstain "frac. abstain from voting, weight by bill"
la var abstain_Vwgt "frac. abstain from voting, weight by vote"
la var absentORabstain "frac. abstain/absent from voting, weight by bill"
la var absentORabstain_Vwgt "frac. abstain/absent from voting, weight by vote"
*/
tempfile voteLHS
save `voteLHS'


// Create compliance variable (non-compliance reverse-coded)
*foreach var of varlist non_compliance* {
*	local newname = substr("`var'",5,.)
*	g `newname' = 1 - `var'
*}

// Create vote difference variable (vote similarity reverse-coded)
foreach var of varlist voteSim4* {
	local newname = substr("`var'",8,.)
	g voteDiff`newname' = 3 - `var'
}
foreach var of varlist voteSim3* {
	local newname = substr("`var'",8,.)
	g voteDiff`newname' = 2 - `var'
}

foreach var of varlist voteSim4c`drawstart'_Vwgt-voteDiff3c_saia`drawend'_VS {
	ren `var' `var'_sim
}

save "`votingsimfolder'/sim_MP_sess_RI${drawstart}to${drawend}_`pcuelab'", replace

timer off 1
timer list 1
beep